package com.project.chat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.chat.pojo.Room;
import com.project.chat.pojo.SettingTool;
import com.project.chat.service.GameService;
import com.project.chat.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("game")
public class GameController {

    @Autowired
    GameService gameService;

    /**
     * 获取某一房间的信息
     */
    @GetMapping("ansjs/123skjdo21/saisdd")
    public String getRoom(int id){
        return JSON.toJSONString(Response.checkNull(gameService.getRoom(id)));
    }

    /**
     * 创建房间
     */
    @PostMapping("ssadiu23748d7dhuds7d82b3/sd/asd/8s")
    public String create(int chiHuiId, int pmNum, int wdNum){

        SettingTool settingTool = SettingTool.createSettingTool(chiHuiId, pmNum, wdNum);
        if(settingTool == null) return JSON.toJSONString(Response.fail());
        int roomId;
        try{
             roomId = gameService.createRoom(settingTool);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return JSON.toJSONString(Response.fail());
        }
        return JSON.toJSONString(Response.success(roomId));
    }

    /**
     * 加入房间
     */
    @GetMapping("jksadowqieosdlasiue/{id}/23/98a/{username}/*")
    public String joinRoom(@PathVariable int id, @PathVariable String username){
        if (gameService.joinRoom(id, username)){
            return JSON.toJSONString(Response.success(null));
        }
        return JSON.toJSONString(Response.fail());
    }

    /**
     * 结束游戏-删除房间
     */
    @GetMapping("i23sjid9sudiahu21u3o12/21asdasd2/{id}/123sd")
    public String deleteRoom(@PathVariable int id){
        gameService.deleteRoom(id);
        return JSON.toJSONString(Response.success(null));
    }

    /**
     * 退出房间
     */
    @GetMapping("sdsdwqiuiuc9892sd/d92/2/{id}/*/{username}/23s")
    public String exitRoom(@PathVariable int id, @PathVariable String username){
        gameService.exitRoom(username, id);
        return JSON.toJSONString(Response.success(null));
    }


}
